#include<reg52.h>
#include"hgmcu.h"
#define uchar unsigned char
#define uint unsigned int
void InitUART(void);
void sendChar(uchar Value);
void xs(uint num,uchar t);
void delay(uchar z);
void display(uchar bai,uchar shi,uchar ge,uchar cyc,uchar time);
uchar code smgsz[]={
0xC0,0xF9,0xA4,0xB0,
0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,
0xC6,0xA1,0x86,0x8E};
uchar time,sendFlag;
void delay58us(void);
void delay100ms(void);
void main(void)  
{
	InitUART();
	P13=0;
	P11=0;
  InitUART();
	delay100ms();
  while(1)
  {
		P11=1;
    delay58us();
		P11=0;
		while(!P12);
		while(P12)
		{
			delay58us();
			time++;
		}
		sendChar(time);
		xs(time,50);
		time=0;
  }
}
void delay58us(void)   //?? -0.25us
{
    unsigned char a,b;
    for(b=1;b>0;b--)
        for(a=74;a>0;a--);
}
void delay100ms(void)   //?? -0.0625us
{
    unsigned char a,b,c;
    for(c=95;c>0;c--)
        for(b=181;b>0;b--)
            for(a=14;a>0;a--);
}
void InitUART(void)
{
    TMOD = 0x20;
    SCON = 0x50;
    TH1 = 0xF7;
    TL1 = TH1;
    PCON = 0x80;
    EA = 1;
    ES = 1;
    TR1 = 1;
}
void serInt() interrupt 4//????
{
    if(RI)
    {
        RI = 0;
    }
    if(TI)  
    {
        TI = 0;
        sendFlag = 0;
    }  
}
void sendChar(uchar Value)  //??
{
     SBUF = Value;       
     sendFlag = 1;
     while(sendFlag);
}
void xs(uint num,uchar t)
{
	uchar bai,shi,ge;
	ge=num%10;
	shi=num%100/10;
	bai=num%1000/100;
	ge=smgsz[ge];
	shi=smgsz[shi];
	bai=smgsz[bai];
	display(bai,shi,ge,t,3);
}
void delay(uchar z)   //?? -0.0625us
{
	unsigned char a,b,x;
	for(x=z;x>0;x--)
  		for(b=130;b>0;b--)
      	 		for(a=19;a>0;a--);
}
void display(uchar bai,uchar shi,uchar ge,uchar cyc,uchar time)
{
uchar xh;
for(xh=0;xh<cyc;xh++)
		{
			P0=bai;//????
			P21=0;
			delay(time);
			P21=1;
			P0=shi;//????
			P22=0;
			delay(time);
			P22=1;
			P0=ge;//????
			P23=0;
			delay(time);
			P23=1;
		}
}